package com.appbee.core.client.msg.ui;


import javax.inject.Inject;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class MessageView extends Composite implements IMessageView
{

	private static MessageViewUiBinder uiBinder = GWT
			.create(MessageViewUiBinder.class);

	interface MessageViewUiBinder extends UiBinder<Widget, MessageView>
	{
	}
	
	@UiField
	DivElement messageElm;
	
	
	@UiField
	DivElement messageText;
	
	@UiField(provided = true)
	MessageStyle style;

	@Inject
	public MessageView(MessageStyle style)
	{
		this.style = style;
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	@Override
	public void showError(SafeHtml message,Throwable caught)
	{
	    SafeHtmlBuilder b = new SafeHtmlBuilder();
	    b.append(message);
	    if(caught.getMessage()!=null)
	    {
	    	b.append(SafeHtmlUtils.fromString(", message: "));
	    	b.appendEscaped(caught.getMessage());
	    }
		showError(b.toSafeHtml());
	}
	@Override
	public void showError(SafeHtml message)
	{
		messageElm.setClassName(style.message());
		messageText.setClassName(style.errortext());
		//icon.setClassName(style.erroricon());
		messageText.setInnerHTML(message.asString());
	}

	@Override
	public void showInfo(SafeHtml message)
	{
		messageElm.setClassName(style.message());
		messageText.setClassName(style.infotext());
		//icon.setClassName(style.infoicon());
		messageText.setInnerHTML(message.asString());
	}

	@Override
	public void showWarning(SafeHtml message)
	{
		messageElm.setClassName(style.message());
		messageText.setClassName(style.warntext());
		//icon.setClassName(style.warnicon());
		messageText.setInnerHTML(message.asString());
	}

	@Override
	public void clearMessage()
	{
		messageElm.setClassName(style.hidden());
		messageText.setInnerHTML("");
	}
	
}
